/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UserInterface.Report;

import Business.Launcher.Launch;
import Business.Record.Record;
import Business.RecordsOperation.RecordsOperation;
import Business.ShowRegion.ShowRegion;
import java.awt.event.WindowEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author hooting
 */
public class ReportJFrame extends javax.swing.JFrame {

    /**
     * Creates new form ReportJFrame
     */
    public ReportJFrame(){
        initComponents();
        this.setLocationRelativeTo(null);
		printRegion();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGroupParticipant = new javax.swing.ButtonGroup();
        btnGroupEventType = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        rdoHospital = new javax.swing.JRadioButton();
        rdoClinics = new javax.swing.JRadioButton();
        rdoGov = new javax.swing.JRadioButton();
        rdoPersonal = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        rdoDevice = new javax.swing.JRadioButton();
        rdoProcedure = new javax.swing.JRadioButton();
        rdoHumanMistake = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtTitle = new javax.swing.JTextField();
        btnCommit = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtDescription = new javax.swing.JTextArea();
        btnReturn = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        cmbCountry = new javax.swing.JComboBox();
        cmbProvincy = new javax.swing.JComboBox();
        cmbHospital = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Participant type:");

        btnGroupParticipant.add(rdoHospital);
        rdoHospital.setText("Hospital");

        btnGroupParticipant.add(rdoClinics);
        rdoClinics.setText("Clinics");

        btnGroupParticipant.add(rdoGov);
        rdoGov.setText("Government");

        btnGroupParticipant.add(rdoPersonal);
        rdoPersonal.setText("Personal");

        jLabel2.setText("Adverse event type:");

        btnGroupEventType.add(rdoDevice);
        rdoDevice.setText("Device");

        btnGroupEventType.add(rdoProcedure);
        rdoProcedure.setText("Clinical procedure");

        btnGroupEventType.add(rdoHumanMistake);
        rdoHumanMistake.setText("Human Mistake");

        jLabel3.setText("Description");

        jLabel4.setText("Title");

        btnCommit.setText("Commit");
        btnCommit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCommitActionPerformed(evt);
            }
        });

        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        txtDescription.setColumns(20);
        txtDescription.setLineWrap(true);
        txtDescription.setRows(5);
        jScrollPane2.setViewportView(txtDescription);

        btnReturn.setText("Return");
        btnReturn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReturnActionPerformed(evt);
            }
        });

        jLabel6.setText("Country");

        jLabel7.setText("Provincy/State");

        jLabel8.setText("Hospital");

        cmbCountry.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbCountryItemStateChanged(evt);
            }
        });

        cmbProvincy.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbProvincyItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(rdoHospital, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(rdoClinics)
                                        .addGap(18, 18, 18)
                                        .addComponent(rdoGov)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(rdoPersonal))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(rdoDevice)
                                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(cmbCountry, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(rdoProcedure)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(12, 12, 12)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(jLabel7)
                                                    .addComponent(cmbProvincy, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(rdoHumanMistake)
                                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(cmbHospital, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(70, 70, 70)
                                .addComponent(btnCommit)
                                .addGap(45, 45, 45)
                                .addComponent(btnReset)
                                .addGap(51, 51, 51)
                                .addComponent(btnReturn)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2)
                            .addComponent(txtTitle)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rdoHospital)
                    .addComponent(rdoClinics)
                    .addComponent(rdoGov)
                    .addComponent(rdoPersonal))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rdoDevice)
                    .addComponent(rdoProcedure)
                    .addComponent(rdoHumanMistake))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbCountry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbProvincy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbHospital, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCommit)
                    .addComponent(btnReset)
                    .addComponent(btnReturn))
                .addGap(25, 25, 25))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

        private void btnCommitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCommitActionPerformed
			if(btnGroupEventType.getSelection() == null){
				JOptionPane.showMessageDialog(null,"Please choose adverse event type!", 
					"warning", JOptionPane.WARNING_MESSAGE);
				return;
			}
			if(btnGroupParticipant.getSelection() == null){
				JOptionPane.showMessageDialog(null,"Please choose participant type!", 
					"warning", JOptionPane.WARNING_MESSAGE);
				return;
			}
			try{
				String title = txtTitle.getText();
				String description = txtDescription.getText();
				description = description.replaceAll("\n\r","").replaceAll("\n","");
				if(title.length() == 0 || description.length() == 0){ 
					throw new NullPointerException();
				}
				String participantType = null;
				if(rdoHospital.isSelected()){
					participantType = "Hospital";
				}else if(rdoClinics.isSelected()){
					participantType = "Clinics";
				}else if(rdoGov.isSelected()){
					participantType = "Government";
				}else if(rdoPersonal.isSelected()){
					participantType = "Personal";
				}
				String eventType = null;
				if(rdoDevice.isSelected()){
					eventType = "Device";
				}else if(rdoProcedure.isSelected()){
					eventType = "Clinical procedure";
				}else if(rdoHumanMistake.isSelected()){
					eventType = "Human mistake";
				}
				String country = (String)cmbCountry.getSelectedItem();
				String provincy = (String)cmbProvincy.getSelectedItem();
				String hospital = (String)cmbHospital.getSelectedItem();
				Record r = new Record(participantType, eventType, title, description,
					country, provincy, hospital);
				new RecordsOperation().insertRecordToDB(r);
				JOptionPane.showMessageDialog(null,"Report problem sucessfully!",
					"Notification", JOptionPane.INFORMATION_MESSAGE);
			}catch(NullPointerException e){
				JOptionPane.showMessageDialog(null,"Please input problem title "
					+ "and description!", 
					"warning", JOptionPane.WARNING_MESSAGE);
			}
        }//GEN-LAST:event_btnCommitActionPerformed

        private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
                // TODO add your handling code here:
			btnGroupParticipant.clearSelection();
			btnGroupEventType.clearSelection();
			txtTitle.setText("");
			txtDescription.setText("");
        }//GEN-LAST:event_btnResetActionPerformed
		
        private void btnReturnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReturnActionPerformed
                // TODO add your handling code here:
			dispose();
			Launch.loginJFrame.setVisible(true);
        }//GEN-LAST:event_btnReturnActionPerformed

    private void cmbCountryItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbCountryItemStateChanged
        // TODO add your handling code here:
		showProvincy();
		showHospital();
    }//GEN-LAST:event_cmbCountryItemStateChanged

    private void cmbProvincyItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbProvincyItemStateChanged
        // TODO add your handling code here:
		showHospital();
    }//GEN-LAST:event_cmbProvincyItemStateChanged
    @Override
	protected void processWindowEvent(WindowEvent e){
		if(e.getID() == WindowEvent.WINDOW_CLOSING){
		}else{
			super.processWindowEvent(e);
		}
	}
	
	private void printRegion(){
		showCountry();
		showProvincy();
		showHospital();
	}
	private void showCountry(){
		String[] countries = new ShowRegion().getCountries();
		if(countries == null){
			return;
		}
		cmbCountry.setModel(new DefaultComboBoxModel(countries));
	}

	private void showProvincy(){
		String country = (String)cmbCountry.getSelectedItem();
		if(country == null || country.length() == 0){
			return;
		}
		String[] provincies = new ShowRegion().getProvincies(country);
		if(provincies == null){
			return;
		}
		cmbProvincy.setModel(new DefaultComboBoxModel(provincies));
	}

	private void showHospital(){
		String country = (String)cmbCountry.getSelectedItem();
		if(country == null || country.length() == 0){
			return;
		}
		String provincy = (String)cmbProvincy.getSelectedItem();
		if(provincy == null || provincy.length() == 0){
			return;
		}
		String[] hospitals = new ShowRegion().getHospitals(country, provincy);
		if(hospitals == null){
			return;
		}
		cmbHospital.setModel(new DefaultComboBoxModel(hospitals));
	}

  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCommit;
    private javax.swing.ButtonGroup btnGroupEventType;
    private javax.swing.ButtonGroup btnGroupParticipant;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnReturn;
    private javax.swing.JComboBox cmbCountry;
    private javax.swing.JComboBox cmbHospital;
    private javax.swing.JComboBox cmbProvincy;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JRadioButton rdoClinics;
    private javax.swing.JRadioButton rdoDevice;
    private javax.swing.JRadioButton rdoGov;
    private javax.swing.JRadioButton rdoHospital;
    private javax.swing.JRadioButton rdoHumanMistake;
    private javax.swing.JRadioButton rdoPersonal;
    private javax.swing.JRadioButton rdoProcedure;
    private javax.swing.JTextArea txtDescription;
    private javax.swing.JTextField txtTitle;
    // End of variables declaration//GEN-END:variables
}
